﻿//=======================================================
// 作者：KingSley
// 描述：模型类，封装应用程序状态，响应状态查询，应用程序功能，通知视图改变
//=======================================================
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace MVC
{
    /// <summary>
    /// 模型委托（当用户信息发生变化时执行）
    /// </summary>
    public delegate void OnValueChange(int val);

    // model是双向的，中间的
    public class PlayerMsgModel : MonoBehaviour
    {
        // 玩家等级
        private int playerLevel;
        // 玩家经验
        private int playerExperience;
        // 玩家升级经验
        private int playerFullExperience;
        // 金币数量
        private int goldNum;
        // 声明委托对象，接收当等级发生变化时，触发的事件
        public OnValueChange OnLevelChange;
        // 声明委托对象，接收当经验发生变化时，触发的事件
        public OnValueChange OnExperienceChange;
        // 声明委托对象，接收当升级经验发生变化时，触发的事件
        public OnValueChange OnFullExperienceChange;
        // 声明委托对象，接收当金币数量发生变化时，触发的事件
        public OnValueChange OnGoldNumChange;

        // 单例
        private static PlayerMsgModel mod;

        /// <summary>
        /// 获取单例的值
        /// </summary>
        /// <returns>Player Msg Model</returns>
        public static PlayerMsgModel GetMod()
        {
            return mod;
        }

        /// <summary>
        /// 程序开始时初始化单例
        /// </summary>
        private void Awake()
        {
            mod = new PlayerMsgModel();
        }

        /// <summary>
        /// 玩家等级属性
        /// </summary>
        /// <value>The player level.</value>
        public int PlayerLevel
        {
            get
            {
                return playerLevel;
            }
            set
            {
                playerLevel = value;
                // 如果委托对象不为空
                if (OnLevelChange != null)
                {
                    // 执行委托
                    OnLevelChange(playerLevel);
                }
            }
        }

        /// <summary>
        /// 玩家经验属性
        /// </summary>
        /// <value>The player experience.</value>
        public int PlayerExperience
        {
            get
            {
                return playerExperience;
            }
            set
            {
                playerExperience = value;
                if (OnExperienceChange != null)
                {
                    OnExperienceChange(playerExperience);
                }
            }
        }

        /// <summary>
        /// 玩家升级经验属性
        /// </summary>
        /// <value>The player full experience.</value>
        public int PlayerFullExperience
        {
            get
            {
                return playerFullExperience;
            }
            set
            {
                playerFullExperience = value;
                if (OnFullExperienceChange != null)
                {
                    OnFullExperienceChange(playerFullExperience);
                }
            }
        }

        /// <summary>
        /// 金币数量属性
        /// </summary>
        /// <value>The gold number.</value>
        public int GoldNum
        {
            get
            {
                return goldNum;
            }
            set
            {
                goldNum = value;
                if (OnGoldNumChange != null)
                {
                    OnGoldNumChange(goldNum);
                }
            }
        }
    }
}
